<template>
  <div id="phone">
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="phone"
        label="手机号"
        type="number"
        maxlength="11"
        placeholder="请输入手机号"
        :rules="[{ required: true, message: '请输入手机号' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请输入密码' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="danger" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
    <router-link to="/reg" class="reg">去注册</router-link>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { Toast } from 'vant'
export default {
  name: 'phoneLogin',
  data () {
    return {
      username: '',
      password: ''
    }
  },

  // 定义方法
  methods: {
    ...mapActions(['user/loginInfo']),
    async onSubmit (values) {
      try {
        await this['user/loginInfo'](values)
        this.$router.push('/user')
      } catch (err) {
        Toast('用户名或密码错误！')
      }
    }
  }
}
</script>

<style scoped lang="less">
.reg {
  display: block;
  font-size: 14px;
  color: #ee0a24;
  text-align: center;
  margin-top: 15px;
}
</style>
